package org.codegallery.javagal.xml.parse;

import org.codegallery.javagal.Constants;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: gongwenwei
 * Date: 11-10-19
 * Time: 下午1:35
 * To change this template use File | Settings | File Templates.
 */
public class SaxParser {
    public static void main(String[] args) {

    }

    public void parse() throws Exception {
        File file = new File(Constants.TEST_RES_DIR + "\\xml\\note.xml");
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser parser = null;
        spf.setNamespaceAware(true);
        //spf.setValidating(true);
        System.out.println("Parser will " + (spf.isNamespaceAware() ? "" : "not ")
                + "be namespace aware");
        System.out.println("Parser will " + (spf.isValidating() ? "" : "not ")
                + "validate XML");

        parser = spf.newSAXParser();
        System.out.println("Parser object is: " + parser);
        XMLReader xmlReader=parser.getXMLReader();
        MyErrorHandler errorHandler=new MyErrorHandler();
        xmlReader.setErrorHandler(errorHandler);
        MySAXHandler handler = new MySAXHandler();
        parser.parse(file, handler);
    }


}

class MyErrorHandler implements ErrorHandler {
  public void warning(SAXParseException e) throws SAXException {
    show("Warning", e);
    throw (e);
  }

  public void error(SAXParseException e) throws SAXException {
    show("Error", e);
    throw (e);
  }

  public void fatalError(SAXParseException e) throws SAXException {
    show("Fatal Error", e);
    throw (e);
  }

  private void show(String type, SAXParseException e) {
    System.out.println(type + ": " + e.getMessage());
    System.out.println("Line " + e.getLineNumber() + " Column "
        + e.getColumnNumber());
    System.out.println("System ID: " + e.getSystemId());
  }
}

class MySAXHandler extends DefaultHandler {
    public void startDocument() {
        System.out.println("Start document: ");
    }

    public void endDocument() {
        System.out.println("End document: ");
    }

    public void startElement(String uri, String localName, String qname,
                             Attributes attr) {
        System.out.println("Start element: local name: " + localName + " qname: "
                + qname + " uri: " + uri);
    }

    public void endElement(String uri, String localName, String qname) {
        System.out.println("End element: local name: " + localName + " qname: "
                + qname + " uri: " + uri);
    }

    public void characters(char[] ch, int start, int length) {
        System.out.println("Characters: " + new String(ch, start, length));
    }

    public void ignorableWhitespace(char[] ch, int start, int length) {
        System.out
                .println("Ignorable whitespace: " + new String(ch, start, length));
    }
}
